package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("userLogin")
public class LoginController {
    private Logger logger;

    @Resource
    private SysUserService sysUserService;

    public LoginController() {  }

    @RequestMapping({"/toLogin"})
    public String login()
    {
        logger.debug("跳转到登录页面");
        return "login";
    }

    @PostMapping({"/login"})
    public String doLogin(@RequestParam String account, @RequestParam String password, HttpServletRequest request, HttpSession session) throws Exception
    {
        logger.debug("登录方法");
        SysUser sysUser=sysUserService.login(account,password);
        if (null!=sysUser)
        {
            //登录成功,重定向到user/tomain接口
            return "redirect:user/toMain";
        }else
            {
                //用户不存在,登录失败,跳转到登录页面
                return "login";
            }
    }

    @RequestMapping({"/logout"})
    public String logout(HttpSession session) { return null;}

    @RequestMapping({"toMain"})
    public String toMain() {
        return "frame";
    }

    @RequestMapping({"/toSysError"})
    public String sysError() { return null;}
}
